package com.xbongbong.paas.business.rule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.BusinessRuleConstant;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.BusinessRuleInfoEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.RuleOperationEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ConditionListHelp;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.help.symbol.AbstractSymbolHandle;
import com.xbongbong.paas.model.BusinessRuleInfoModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.ActiveRulePoJo;
import com.xbongbong.paas.pojo.BusinessRulesPoJo;
import com.xbongbong.paas.pojo.OperationsPojo;
import com.xbongbong.paas.pojo.RulePoJo;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.RegexUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.util.ConditionUtil;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.constant.PackageConstant;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.pro.enums.businessrule.enums.RuleValueTypeEnum;
import com.xbongbong.pro.enums.businessrule.enums.RuleVersionEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.form.pojo.RuleTargetPojo;
import com.xbongbong.pro.form.pojo.dto.DeleteBusinessRuleInfoDTO;
import com.xbongbong.pro.form.pojo.dto.EnableBusinessRuleInfoDTO;
import com.xbongbong.pro.form.pojo.dto.FormUpdateBusinessRuleDTO;
import com.xbongbong.pro.form.pojo.dto.SortBusinessRuleInfoDTO;
import com.xbongbong.pro.form.pojo.vo.DeleteBusinessRuleInfoVO;
import com.xbongbong.pro.form.pojo.vo.EnableBusinessRuleInfoVO;
import com.xbongbong.pro.form.pojo.vo.FormBusinessRuleVO;
import com.xbongbong.pro.form.pojo.vo.FormUpdateBusinessRuleVO;
import com.xbongbong.pro.form.pojo.vo.SortBusinessRuleInfoVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.domain.entity.WorkOrderFormEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderFormModel;
import com.xbongbong.saas.service.toolbox.help.UpdatePackageRedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/6/9 20:04
 * @since v1.0
 */
@Service("businessRuleInfoService")
public class BusinessRuleInfoServiceImpl implements BusinessRuleInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessRuleInfoServiceImpl.class);


    @Resource
    private BusinessRuleInfoModel businessRuleInfoModel;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private WorkOrderFormModel workOrderFormModel;
    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private BusinessRuleUserPermissionHelp businessRuleUserPermissionHelp;
    @Resource
    private PackageHelp packageHelp;
    @Resource
    private UpdatePackageRedisService updatePackageRedisService;
    @Resource
    private PaasRedisHelper paasRedisHelper;


    @Override
    public FormBusinessRuleVO get(String corpid, Long formId, Integer businessType, Integer saasMark, Integer searchFormName) throws XbbException {
        List<BusinessRulesPoJo> businessRuleList = new ArrayList<>();
        FormBusinessRuleVO formBusinessRuleVO = new FormBusinessRuleVO();
        try {
            Map<String, Object> param1 = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param1.put(StringConstant.CORPID, corpid);
            param1.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            param1.put(StringConstant.BUSINESS_TYPE, businessType);
            param1.put(StringConstant.FORM_ID, formId);
            param1.put("orderByStr", "priority");
            List<BusinessRuleInfoEntity> businessRuleInfo = businessRuleInfoModel.findEntitys(param1);
            if (CollectionsUtil.isEmpty(businessRuleInfo)) {
                formBusinessRuleVO.setFormulaLimit(false);
                formBusinessRuleVO.setBusinessRule(businessRuleList);
                return formBusinessRuleVO;
            }
            Set<Long> formIdIn = new HashSet<>();
            Set<Long> workOrderFormIdIn = new HashSet<>();
            businessRuleInfo.forEach(item->{
                getFormIdIn(item.getActiveFormId(), item.getActiveBusinessType(), formIdIn, workOrderFormIdIn);
                getFormIdIn(item.getInactiveFormId(), item.getActiveBusinessType(), formIdIn, workOrderFormIdIn);
            });
            Map<Long, WorkOrderFormEntity> workOrderFormMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<Long, PaasFormEntityExt> formMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.equals(1, searchFormName)) {
                getFormMap(formIdIn, workOrderFormIdIn, corpid, workOrderFormMap, formMap);
            }
            Integer feeType = packageHelp.getFeeType(corpid);
            formBusinessRuleVO.setFormulaLimit(false);
            businessRuleInfo.forEach(item->{
                BusinessRulesPoJo businessRulesPoJo = new BusinessRulesPoJo();
                // 生效
                if (StringUtil.isNotEmpty(item.getActiveName())) {
                    String name = getFormName(item.getActiveBusinessType(), item.getActiveFormId(), workOrderFormMap, formMap);
                    RuleTargetPojo ruleTargetPojo = new RuleTargetPojo(item.getActiveAppId(), item.getActiveMenuId(), item.getActiveFormId(), name, item.getActiveSaasMark(),
                            item.getActiveBusinessType());
                    RulePoJo rulePoJo = new RulePoJo(ruleTargetPojo, item.getActiveOperationType());
                    // 前置过滤条件
                    rulePoJo.setPreConditions(JSON.parseArray(item.getActivePreConditions(), FilterConditionsPoJo.class));
                    // 过滤条件
                    rulePoJo.setFilterConditions(JSON.parseArray(item.getActiveFilterConditions(), FilterConditionsPoJo.class));
                    rulePoJo.setOperations(JSON.parseArray(item.getActiveOperations(), OperationsPojo.class));
                    ActiveRulePoJo activeRulePoJo = new ActiveRulePoJo(item.getActiveName(), item.getActiveType(), rulePoJo, item.getActiveMemo(), item.getActiveEnable());
                    Boolean packageLimit = setPackageLimit(activeRulePoJo, feeType);
                    if (packageLimit) {
                        formBusinessRuleVO.setFormulaLimit(true);
                    }
                    businessRulesPoJo.setActiveRule(activeRulePoJo);
                }
                // 失效
                if (StringUtil.isNotEmpty(item.getInactiveName())) {
                    String inactiveFormName = getFormName(item.getInactiveBusinessType(), item.getInactiveFormId(), workOrderFormMap, formMap);
                    RuleTargetPojo ruleTargetPojo = new RuleTargetPojo(item.getInactiveAppId(), item.getInactiveMenuId(), item.getInactiveFormId(), inactiveFormName, item.getInactiveSaasMark(),
                            item.getInactiveBusinessType());
                    RulePoJo rulePoJo = new RulePoJo(ruleTargetPojo, item.getInactiveOperationType());
                    rulePoJo.setPreConditions(JSON.parseArray(item.getInactivePreConditions(), FilterConditionsPoJo.class));
                    rulePoJo.setFilterConditions(JSON.parseArray(item.getInactiveFilterConditions(), FilterConditionsPoJo.class));
                    rulePoJo.setOperations(JSON.parseArray(item.getInactiveOperations(), OperationsPojo.class));
                    ActiveRulePoJo inactiveRulePoJo = new ActiveRulePoJo(item.getInactiveName(), item.getInactiveType(), rulePoJo, item.getInactiveMemo(), item.getInactiveEnable());
                    Boolean packageLimit = setPackageLimit(inactiveRulePoJo, feeType);
                    if (packageLimit) {
                        formBusinessRuleVO.setFormulaLimit(true);
                    }
                    businessRulesPoJo.setInactiveRule(inactiveRulePoJo);
                }
                businessRulesPoJo.setPriority(item.getPriority());
                businessRulesPoJo.setBusinessRuleId(item.getId());
                businessRuleList.add(businessRulesPoJo);
            });
            Integer num =  updatePackageRedisService.getUnusedNumPackageLimit(corpid, ValueAddedTypeEnum.BUSINESS_RULES);
            if (num>0) {
                formBusinessRuleVO.setMessage(String.format(I18nMessageUtil.getMessage(PackageConstant.BUSINESS_RULE_SIZE_MESSAGE), num));
            }
            formBusinessRuleVO.setExcess(num);
            formBusinessRuleVO.setBusinessRule(businessRuleList);
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.get出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return formBusinessRuleVO;
    }

    @Override
    public void setBusinessRule(List<String> corpidIn) throws XbbException {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpidIn", corpidIn);
        param.put("businessRuleGte", "[]");
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        List<PaasFormEntityExt> formList = paasFormModel.findEntitys(param);
        List<BusinessRuleInfoEntity> businessRuleInfoList = new ArrayList<>();
        formList.forEach(item->{
           List<BusinessRulesPoJo> businessRuleList = JSON.parseArray(item.getBusinessRules(), BusinessRulesPoJo.class);
           if (Objects.nonNull(businessRuleList)) {
               String creatorId = item.getCreatorId();
               Integer saasMark = item.getSaasMark();
               Integer businessType = item.getBusinessType();
               Long formId = item.getId();
               Long appId = item.getAppId();
               Long menuId = item.getMenuId();
               String corpid = item.getCorpid();
               businessRuleList.forEach(businessRule->{
                   long now = DateTimeUtil.getInt();
                   BusinessRuleInfoEntity businessRuleInfoEntity = new BusinessRuleInfoEntity();
                   businessRuleInfoEntity.setCorpid(corpid);
                   businessRuleInfoEntity.setFormId(formId);
                   businessRuleInfoEntity.setAppId(appId);
                   businessRuleInfoEntity.setMenuId(menuId);
                   businessRuleInfoEntity.setSaasMark(saasMark);
                   businessRuleInfoEntity.setBusinessType(businessType);
                   ActiveRulePoJo activeRulePoJo = businessRule.getActiveRule();
                   if (Objects.nonNull(activeRulePoJo)) {
                       RulePoJo rulePoJo = activeRulePoJo.getRule();
                       RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();
                       businessRuleInfoEntity.setActiveName(activeRulePoJo.getName());
                       businessRuleInfoEntity.setActiveType(activeRulePoJo.getType());
                       businessRuleInfoEntity.setActiveAppId(ruleTargetPojo.getAppId());
                       businessRuleInfoEntity.setActiveMenuId(ruleTargetPojo.getMenuId());
                       businessRuleInfoEntity.setActiveFormId(ruleTargetPojo.getFormId());
                       businessRuleInfoEntity.setActiveSaasMark(ruleTargetPojo.getLinkSaasMark());
                       businessRuleInfoEntity.setActiveBusinessType(ruleTargetPojo.getLinkBusinessType());
                       businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(rulePoJo.getFilterConditions()));
                       businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(rulePoJo.getOperations()));
                       businessRuleInfoEntity.setActivePreConditions(JSON.toJSONString(rulePoJo.getPreConditions()));
                       businessRuleInfoEntity.setActiveOperationType(rulePoJo.getOperationType());
                       businessRuleInfoEntity.setActiveMemo(activeRulePoJo.getMemo());
                       businessRuleInfoEntity.setActiveEnable(activeRulePoJo.getEnable());
                   } else {
                       businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                       businessRuleInfoEntity.setActivePreConditions(JSON.toJSONString(new ArrayList<>()));
                       businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(new ArrayList<>()));
                   }
                   ActiveRulePoJo inactiveRule = businessRule.getInactiveRule();
                   if (Objects.nonNull(inactiveRule)) {
                       RulePoJo rulePoJo = inactiveRule.getRule();
                       RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();

                       businessRuleInfoEntity.setInactiveName(inactiveRule.getName());
                       businessRuleInfoEntity.setInactiveType(inactiveRule.getType());
                       businessRuleInfoEntity.setInactiveAppId(ruleTargetPojo.getAppId());
                       businessRuleInfoEntity.setInactiveMenuId(ruleTargetPojo.getMenuId());
                       businessRuleInfoEntity.setInactiveFormId(ruleTargetPojo.getFormId());
                       businessRuleInfoEntity.setInactiveSaasMark(ruleTargetPojo.getLinkSaasMark());
                       businessRuleInfoEntity.setInactiveBusinessType(ruleTargetPojo.getLinkBusinessType());
                       businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(rulePoJo.getFilterConditions()));
                       businessRuleInfoEntity.setInactivePreConditions(JSON.toJSONString(rulePoJo.getPreConditions()));
                       businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(rulePoJo.getOperations()));
                       businessRuleInfoEntity.setInactiveOperationType(rulePoJo.getOperationType());
                       businessRuleInfoEntity.setInactiveMemo(inactiveRule.getMemo());
                       businessRuleInfoEntity.setInactiveEnable(inactiveRule.getEnable());
                   } else {
                       businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                       businessRuleInfoEntity.setInactivePreConditions(JSON.toJSONString(new ArrayList<>()));
                       businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(new ArrayList<>()));
                   }
                   businessRuleInfoEntity.setCreatorId(creatorId);
                   businessRuleInfoEntity.setDel(DelEnum.NORMAL.getDel());
                   businessRuleInfoEntity.setAddTime(now);
                   businessRuleInfoEntity.setUpdateTime(now);
                   businessRuleInfoEntity.setPriority(businessRule.getPriority());
                   businessRuleInfoList.add(businessRuleInfoEntity);
               });
           }
        });
        businessRuleInfoModel.insertBatch(businessRuleInfoList);
    }

    @Override
    public FormUpdateBusinessRuleVO save(FormUpdateBusinessRuleDTO formUpdateBusinessRuleDTO) throws XbbException {
        FormUpdateBusinessRuleVO formUpdateBusinessRuleVO = new FormUpdateBusinessRuleVO();
        Long businessRuleId = formUpdateBusinessRuleDTO.getBusinessRuleId();
        try {
            String corpid = formUpdateBusinessRuleDTO.getCorpid();
            Long formId = formUpdateBusinessRuleDTO.getFormId();
            Integer businessType = formUpdateBusinessRuleDTO.getBusinessType();
            Integer num = updatePackageRedisService.getOverNum(corpid, ValueAddedTypeEnum.BUSINESS_RULES);
            Integer feeType = businessRuleUserPermissionHelp.getFeeType(corpid);
            if (Objects.equals(PackageTypeEnum.STANDARD.getType(), feeType)) {
                // 标准版
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261020);
            }
            if (Objects.isNull(businessRuleId) && Objects.equals(PackageTypeEnum.ADVANCED.getType(), feeType) && num >= 0) {
                // 高级版
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261018);
            }
            if (Objects.isNull(businessRuleId) && Objects.equals(PackageTypeEnum.ULTIMATE.getType(), feeType) && num >= 0) {
                // 旗舰版不大于50条
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261019);
            }
            Map<String, Object> param1 = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param1.put(StringConstant.CORPID, corpid);
            param1.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            param1.put(StringConstant.BUSINESS_TYPE, formUpdateBusinessRuleDTO.getBusinessType());
            param1.put(StringConstant.FORM_ID, formUpdateBusinessRuleDTO.getFormId());
            List<BusinessRuleInfoEntity> businessRuleInfoList = businessRuleInfoModel.findEntitys(param1);
            Integer formCount = businessRuleInfoList.size();
            if (Objects.isNull(businessRuleId) && Objects.equals(PackageTypeEnum.ULTIMATE.getType(), feeType) && formCount >= BusinessRuleConstant.FORM_ULTIMATE) {
                // 旗舰版单个表单不大于10条
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261019);
            }
            PaasFormExplainEntity paasFormExplainEntity = proFormHelp.getExplainEntity(corpid, formId, businessType);
            // 当前表单
            Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(paasFormExplainEntity.getExplains(), null);
            FieldAttrEntity fieldAttrEntity = new FieldAttrEntity();
            fieldAttrEntity.setAttr(FieldTypeEnum.DATAID.getAlias());
            fieldAttrEntity.setAttrName("");
            fieldAttrEntity.setFieldType(FieldTypeEnum.DATAID.getType());
            explainMap.put(FieldTypeEnum.DATAID.getAlias(), fieldAttrEntity);
            BusinessRulesPoJo businessRulesPoJo = formUpdateBusinessRuleDTO.getBusinessRule();

            Set<Long> formIdList = new HashSet<>();
            Set<Long> workOrderFormIdList = new HashSet<>();

            if (Objects.nonNull(businessRulesPoJo.getActiveRule())) {
                RulePoJo rulePoJo = businessRulesPoJo.getActiveRule().getRule();
                RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();
                Integer targetBusinessType = ruleTargetPojo.getLinkBusinessType();
                Long targetFormId = ruleTargetPojo.getFormId();
                if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), targetBusinessType)) {
                    workOrderFormIdList.add(targetFormId);
                } else {
                    formIdList.add(targetFormId);
                }
            }
            if (Objects.nonNull(businessRulesPoJo.getInactiveRule())) {
                RulePoJo rulePoJo = businessRulesPoJo.getInactiveRule().getRule();
                RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();
                Integer targetBusinessType = ruleTargetPojo.getLinkBusinessType();
                Long targetFormId = ruleTargetPojo.getFormId();

                if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), targetBusinessType)) {
                    workOrderFormIdList.add(targetFormId);
                } else {
                    formIdList.add(targetFormId);
                }
            }

            // 目标表单
            Map<Long, PaasFormExplainEntity> targetExplain = proFormHelp.getFormExplainMap(formIdList, corpid);
            Map<Long, WorkOrderExplainEntity> workOrderExplain = proFormHelp.getWorkOrderFormExplainMap(workOrderFormIdList, corpid);
            Map<Long, Map<String, FieldAttrEntity>> targetExplainMap = new HashMap<>(targetExplain.size());
            Iterator<Long> targetFormIterator = targetExplain.keySet().iterator();
            FieldAttrEntity dataIdField = null;
            while (targetFormIterator.hasNext()) {
                Long targetFormId = targetFormIterator.next();
                PaasFormExplainEntity paasFormExplainEntity1 = targetExplain.get(targetFormId);
                Map<String, FieldAttrEntity> targetMap = ExplainUtil.getExplainMap(paasFormExplainEntity1.getExplains(), null);
                dataIdField = new FieldAttrEntity();
                dataIdField.setAttr(FieldTypeEnum.DATAID.getAlias());
                dataIdField.setAttrName("");
                dataIdField.setFieldType(FieldTypeEnum.DATAID.getType());
                targetMap.put(FieldTypeEnum.DATAID.getAlias(), fieldAttrEntity);
                targetExplainMap.put(targetFormId, targetMap);
            }
            Map<Long, Map<String, FieldAttrEntity>> workOrderExplainMap = new HashMap<>(workOrderExplain.size());
            Iterator<Long> targetWorkOrderFormIterator = workOrderExplain.keySet().iterator();
            while (targetWorkOrderFormIterator.hasNext()) {
                Long targetFormId = targetWorkOrderFormIterator.next();
                WorkOrderExplainEntity paasFormExplainEntity1 = workOrderExplain.get(targetFormId);
                Map<String, FieldAttrEntity> targetMap = ExplainUtil.getExplainMap(paasFormExplainEntity1.getExplains(), null);
                dataIdField = new FieldAttrEntity();
                dataIdField.setAttr(FieldTypeEnum.DATAID.getAlias());
                dataIdField.setAttrName("");
                dataIdField.setFieldType(FieldTypeEnum.DATAID.getType());
                targetMap.put(FieldTypeEnum.DATAID.getAlias(), fieldAttrEntity);
                workOrderExplainMap.put(targetFormId, targetMap);
            }
            ActiveRulePoJo activeRulePoJo = businessRulesPoJo.getActiveRule();
            if (Objects.nonNull(activeRulePoJo)) {
                Map<String, Set<String>> conditionMap = validateFilterCondition(activeRulePoJo.getRule(), activeRulePoJo.getType(), explainMap, targetExplainMap, workOrderExplainMap, formId, businessType);
                Map<String, Set<String>> operatorMap = validateOperate(corpid, activeRulePoJo.getRule(), activeRulePoJo.getType(), explainMap, targetExplainMap, workOrderExplainMap, formId, businessType);
                validateSubForm(conditionMap, operatorMap, activeRulePoJo.getRule().getOperationType());
            }
            ActiveRulePoJo inactiveRulePoJo = businessRulesPoJo.getInactiveRule();
            if (Objects.nonNull(inactiveRulePoJo)) {
                Map<String, Set<String>> conditionMap = validateFilterCondition(inactiveRulePoJo.getRule(), inactiveRulePoJo.getType(), explainMap, targetExplainMap, workOrderExplainMap, formId, businessType);
                Map<String, Set<String>> operatorMap = validateOperate(corpid, inactiveRulePoJo.getRule(), inactiveRulePoJo.getType(), explainMap, targetExplainMap, workOrderExplainMap, formId, businessType);
                validateSubForm(conditionMap, operatorMap, inactiveRulePoJo.getRule().getOperationType());
            }
            if (Objects.isNull(businessRuleId)) {
                BusinessRuleInfoEntity businessRuleInfoEntity = add(formUpdateBusinessRuleDTO.getBusinessRule(), formUpdateBusinessRuleDTO, explainMap);
                businessRuleId = businessRuleInfoEntity.getId();
            } else {
                update(formUpdateBusinessRuleDTO.getBusinessRule(), formUpdateBusinessRuleDTO, explainMap);
            }
            String key = corpid + "_"+ ValueAddedTypeEnum.BUSINESS_RULES.getType();
            paasRedisHelper.removeValue(RedisPrefixConstant.VALUE_ADDED, key);
            formUpdateBusinessRuleVO.setBusinessRuleId(businessRuleId);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.save 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return formUpdateBusinessRuleVO;
    }

    @Override
    public DeleteBusinessRuleInfoVO delete(DeleteBusinessRuleInfoDTO deleteBusinessRuleInfoDTO) throws XbbException {
        try {
            BusinessRuleInfoEntity businessRuleInfoEntity = new BusinessRuleInfoEntity();
            String corpid = deleteBusinessRuleInfoDTO.getCorpid();
            Long businessRuleId = deleteBusinessRuleInfoDTO.getBusinessRuleId();
            Integer deleteType = deleteBusinessRuleInfoDTO.getDeleteType();
            businessRuleInfoEntity.setId(businessRuleId);
            businessRuleInfoEntity.setCorpid(corpid);
            if (Objects.equals(1, deleteType)) {
                businessRuleInfoModel.delete(corpid, businessRuleId);
                Map<String, Object> param1 = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param1.put(StringConstant.CORPID, corpid);
                param1.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
                param1.put(StringConstant.BUSINESS_TYPE, deleteBusinessRuleInfoDTO.getBusinessType());
                param1.put(StringConstant.FORM_ID, deleteBusinessRuleInfoDTO.getFormId());
                Integer count = businessRuleInfoModel.getEntitysCount(param1);
                if (Objects.equals(0, count)) {
                    updateForm(deleteBusinessRuleInfoDTO.getBusinessType(), deleteBusinessRuleInfoDTO.getFormId(), corpid, "0");
                }
                String key = corpid + "_"+ ValueAddedTypeEnum.BUSINESS_RULES.getType();
                paasRedisHelper.removeValue(RedisPrefixConstant.VALUE_ADDED, key);
            } else if (Objects.equals(2, deleteType)) {
                businessRuleInfoEntity.setActiveName("");
                businessRuleInfoEntity.setActiveAppId(0L);
                businessRuleInfoEntity.setActiveMenuId(0L);
                businessRuleInfoEntity.setActiveFormId(0L);
                businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActiveEnable(0);
                businessRuleInfoEntity.setActiveMemo("");
            } else if (Objects.equals(3, deleteType)) {
                businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactiveName("");
                businessRuleInfoEntity.setInactiveAppId(0L);
                businessRuleInfoEntity.setInactiveMenuId(0L);
                businessRuleInfoEntity.setInactiveFormId(0L);
                businessRuleInfoEntity.setInactiveEnable(0);
                businessRuleInfoEntity.setInactiveMemo("");
            }
            businessRuleInfoEntity.setDel(DelEnum.NORMAL.getDel());
            businessRuleInfoModel.update(businessRuleInfoEntity);
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.delete 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return new DeleteBusinessRuleInfoVO();
    }


    @Override
    public SortBusinessRuleInfoVO sort(SortBusinessRuleInfoDTO sortBusinessRuleInfoDTO) throws XbbException {
        SortBusinessRuleInfoVO sortBusinessRuleInfoVO = new SortBusinessRuleInfoVO();
        try {
            String corpid = sortBusinessRuleInfoDTO.getCorpid();
            List<BusinessRuleInfoEntity> businessRuleInfoList = new ArrayList<>();
            List<Long> businessRuleIdList = sortBusinessRuleInfoDTO.getBusinessRuleIdList();
            Integer priority = 1;
            for (Long item : businessRuleIdList) {
                BusinessRuleInfoEntity businessRuleInfoEntity = new BusinessRuleInfoEntity();
                businessRuleInfoEntity.setId(item);
                businessRuleInfoEntity.setPriority(priority);
                businessRuleInfoEntity.setUpdateTime(DateTimeUtil.getInt());
                priority++;
                businessRuleInfoList.add(businessRuleInfoEntity);
            }
            if (CollectionsUtil.isNotEmpty(businessRuleInfoList)) {
                businessRuleInfoModel.updatePriority(businessRuleInfoList, corpid);
            }
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.sort 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }

        return sortBusinessRuleInfoVO;
    }

    @Override
    public EnableBusinessRuleInfoVO enable(EnableBusinessRuleInfoDTO enableBusinessRuleInfoDTO) throws XbbException {

        try {
            Integer activeEnable = null;
            Integer inactiveEnable = null;
            Integer ruleType = enableBusinessRuleInfoDTO.getRuleType();
            if (Objects.equals(1, ruleType)) {
                activeEnable = enableBusinessRuleInfoDTO.getEnable();
            } else if (Objects.equals(2, ruleType)) {
                inactiveEnable = enableBusinessRuleInfoDTO.getEnable();
            }
            businessRuleInfoModel.updateEnable(enableBusinessRuleInfoDTO.getBusinessRuleId(), activeEnable,
                    inactiveEnable, enableBusinessRuleInfoDTO.getCorpid());
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.enable 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return new EnableBusinessRuleInfoVO();
    }

    /**
     * 校验表单下是否含有业务规则
     * @param corpid
     * @param formId
     * @param businessType
     * @throws XbbException
     */
    @Override
    public Integer validateBusinessRules(String corpid, Long formId, Integer businessType) throws XbbException {
        Integer feeType = packageHelp.getFeeType(corpid);
        if (!Objects.equals(PackageTypeEnum.STANDARD.getType(), feeType)) {
            // 如果是高级版和旗舰版，需要判断是否有业务规则
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put(StringConstant.CORPID, corpid);
            param.put("deleteBusinessType", businessType);
            param.put("deleteFormId", formId);
            param.put("deleteForm", 1);
            param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<BusinessRuleInfoEntity> list = businessRuleInfoModel.findEntitys(param);
            if (list.size() > 0) {
                Set<Long> formIdIn = new HashSet<>();
                Set<Long> workOrderFormIdIn = new HashSet<>();
                Map<Long, WorkOrderFormEntity> workOrderFormMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long, PaasFormEntityExt> formMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                list.forEach(item->{
                    getFormIdIn(item.getFormId(), item.getBusinessType(), formIdIn, workOrderFormIdIn);
                });
                getFormMap(formIdIn, workOrderFormIdIn, corpid, workOrderFormMap, formMap);
                List<String> formNameList = new ArrayList<>();
                Iterator<Long> iterator = workOrderFormMap.keySet().iterator();
                while (iterator.hasNext()) {
                    WorkOrderFormEntity workOrderFormEntity = workOrderFormMap.get(iterator.next());
                    formNameList.add(workOrderFormEntity.getName());
                }
                Iterator<Long> iterator1 = formMap.keySet().iterator();
                while (iterator1.hasNext()) {
                    PaasFormEntityExt workOrderFormEntity = formMap.get(iterator1.next());
                    formNameList.add(workOrderFormEntity.getName());
                }
                if (CollectionsUtil.isNotEmpty(formNameList)) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261022, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261022.getMsg(), StringUtils.join(formNameList, "，")));
                }
            }
        }
        return feeType;
    }




    private void updateForm(Integer businessType, Long formId, String corpid, String flag) throws XbbException {
        if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), businessType)) {
            WorkOrderFormEntity workOrderFormEntity = new WorkOrderFormEntity();
            workOrderFormEntity.setId(formId);
            workOrderFormEntity.setCorpid(corpid);
            workOrderFormEntity.setBusinessRules(flag);
            workOrderFormModel.update(workOrderFormEntity);
        } else {
            PaasFormEntity updateForm = new PaasFormEntity();
            updateForm.setId(formId);
            updateForm.setCorpid(corpid);
            updateForm.setBusinessRules(flag);
            paasFormModel.update(updateForm);
        }
    }

    private BusinessRuleInfoEntity add(BusinessRulesPoJo businessRule, FormUpdateBusinessRuleDTO formUpdateBusinessRuleDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        BusinessRuleInfoEntity businessRuleInfoEntity = new BusinessRuleInfoEntity();
        try {
            Long formId = formUpdateBusinessRuleDTO.getFormId();
            Long appId = formUpdateBusinessRuleDTO.getAppId();
            Long menuId = formUpdateBusinessRuleDTO.getMenuId();
            Integer saasMark = formUpdateBusinessRuleDTO.getSaasMark();
            Integer businessType = formUpdateBusinessRuleDTO.getBusinessType();
            String corpid = formUpdateBusinessRuleDTO.getCorpid();
            businessRuleInfoEntity.setCorpid(corpid);
            businessRuleInfoEntity.setFormId(formId);
            businessRuleInfoEntity.setAppId(appId);
            businessRuleInfoEntity.setMenuId(menuId);
            businessRuleInfoEntity.setSaasMark(saasMark);
            businessRuleInfoEntity.setBusinessType(businessType);
            ActiveRulePoJo activeRulePoJo = businessRule.getActiveRule();
            if (Objects.nonNull(activeRulePoJo)) {
                // 生效规则
                RulePoJo rulePoJo = activeRulePoJo.getRule();
                // 校验前置过滤条件
                validatePreCondition(rulePoJo.getPreConditions(), explainMap);

                RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();
                businessRuleInfoEntity.setActiveName(activeRulePoJo.getName());
                businessRuleInfoEntity.setActiveType(activeRulePoJo.getType());
                // 生效规则目标表单属性
                businessRuleInfoEntity.setActiveAppId(ruleTargetPojo.getAppId());
                businessRuleInfoEntity.setActiveMenuId(ruleTargetPojo.getMenuId());
                businessRuleInfoEntity.setActiveFormId(ruleTargetPojo.getFormId());
                businessRuleInfoEntity.setActiveSaasMark(ruleTargetPojo.getLinkSaasMark());
                businessRuleInfoEntity.setActiveBusinessType(ruleTargetPojo.getLinkBusinessType());
                // 过滤条件
                businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(rulePoJo.getFilterConditions()));
                // 前置过滤条件
                businessRuleInfoEntity.setActivePreConditions(JSON.toJSONString(rulePoJo.getPreConditions()));
                businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(rulePoJo.getOperations()));
                businessRuleInfoEntity.setActiveOperationType(rulePoJo.getOperationType());
                businessRuleInfoEntity.setActiveMemo(activeRulePoJo.getMemo());
                businessRuleInfoEntity.setActiveEnable(activeRulePoJo.getEnable());
            } else {
                businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActivePreConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActiveEnable(0);
            }
            ActiveRulePoJo inactiveRule = businessRule.getInactiveRule();
            if (Objects.nonNull(inactiveRule)) {
                // 失效规则
                RulePoJo inactiveRuleRule = inactiveRule.getRule();
                // 校验前置过滤条件
                validatePreCondition(inactiveRuleRule.getPreConditions(), explainMap);

                RuleTargetPojo ruleTargetPojo = inactiveRuleRule.getTarget();

                businessRuleInfoEntity.setInactiveName(inactiveRule.getName());
                businessRuleInfoEntity.setInactiveType(inactiveRule.getType());
                // 失效规则目标表单属性
                businessRuleInfoEntity.setInactiveAppId(ruleTargetPojo.getAppId());
                businessRuleInfoEntity.setInactiveMenuId(ruleTargetPojo.getMenuId());
                businessRuleInfoEntity.setInactiveFormId(ruleTargetPojo.getFormId());
                businessRuleInfoEntity.setInactiveSaasMark(ruleTargetPojo.getLinkSaasMark());
                businessRuleInfoEntity.setInactiveBusinessType(ruleTargetPojo.getLinkBusinessType());
                // 过滤条件
                businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(inactiveRuleRule.getFilterConditions()));
                // 前置过滤条件
                businessRuleInfoEntity.setInactivePreConditions(JSON.toJSONString(inactiveRuleRule.getPreConditions()));
                // 操作方式
                businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(inactiveRuleRule.getOperations()));
                businessRuleInfoEntity.setInactiveOperationType(inactiveRuleRule.getOperationType());
                businessRuleInfoEntity.setInactiveMemo(inactiveRule.getMemo());
                businessRuleInfoEntity.setInactiveEnable(inactiveRule.getEnable());
            } else {
                businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactivePreConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactiveEnable(0);
            }
            businessRuleInfoEntity.setCreatorId(formUpdateBusinessRuleDTO.getUserId());
            businessRuleInfoEntity.setDel(DelEnum.NORMAL.getDel());
            businessRuleInfoEntity.setPriority(businessRule.getPriority());
            businessRuleInfoModel.insert(businessRuleInfoEntity);
            updateForm(businessType, formId, corpid, "1");
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.add 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return businessRuleInfoEntity;
    }

    private Integer update(BusinessRulesPoJo businessRule, FormUpdateBusinessRuleDTO formUpdateBusinessRuleDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        Integer update;
        try {
            BusinessRuleInfoEntity businessRuleInfoEntity = new BusinessRuleInfoEntity();
            Long formId = formUpdateBusinessRuleDTO.getFormId();
            Long appId = formUpdateBusinessRuleDTO.getAppId();
            Long menuId = formUpdateBusinessRuleDTO.getMenuId();
            Integer saasMark = formUpdateBusinessRuleDTO.getSaasMark();
            Integer businessType = formUpdateBusinessRuleDTO.getBusinessType();
            String corpid = formUpdateBusinessRuleDTO.getCorpid();
            businessRuleInfoEntity.setId(formUpdateBusinessRuleDTO.getBusinessRuleId());
            businessRuleInfoEntity.setCorpid(corpid);
            businessRuleInfoEntity.setFormId(formId);
            businessRuleInfoEntity.setAppId(appId);
            businessRuleInfoEntity.setMenuId(menuId);
            businessRuleInfoEntity.setSaasMark(saasMark);
            businessRuleInfoEntity.setBusinessType(businessType);
            ActiveRulePoJo activeRulePoJo = businessRule.getActiveRule();
            if (Objects.nonNull(activeRulePoJo)) {
                RulePoJo rulePoJo = activeRulePoJo.getRule();
                // 校验前置过滤条件
                validatePreCondition(rulePoJo.getPreConditions(), explainMap);

                RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();
                businessRuleInfoEntity.setActiveName(activeRulePoJo.getName());
                businessRuleInfoEntity.setActiveType(activeRulePoJo.getType());
                businessRuleInfoEntity.setActiveAppId(ruleTargetPojo.getAppId());
                businessRuleInfoEntity.setActiveMenuId(ruleTargetPojo.getMenuId());
                businessRuleInfoEntity.setActiveFormId(ruleTargetPojo.getFormId());
                businessRuleInfoEntity.setActiveSaasMark(ruleTargetPojo.getLinkSaasMark());
                businessRuleInfoEntity.setActiveBusinessType(ruleTargetPojo.getLinkBusinessType());
                businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(rulePoJo.getFilterConditions()));
                businessRuleInfoEntity.setActivePreConditions(JSON.toJSONString(rulePoJo.getPreConditions()));
                businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(rulePoJo.getOperations()));
                businessRuleInfoEntity.setActiveOperationType(rulePoJo.getOperationType());
                businessRuleInfoEntity.setActiveMemo(activeRulePoJo.getMemo());
                businessRuleInfoEntity.setActiveEnable(activeRulePoJo.getEnable());
            } else {
                businessRuleInfoEntity.setActiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActivePreConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setActiveOperations(JSON.toJSONString(new ArrayList<>()));
            }
            ActiveRulePoJo inactiveRule = businessRule.getInactiveRule();
            if (Objects.nonNull(inactiveRule)) {
                RulePoJo rulePoJo = inactiveRule.getRule();
                RuleTargetPojo ruleTargetPojo = rulePoJo.getTarget();
                // 校验前置过滤条件
                validatePreCondition(rulePoJo.getPreConditions(), explainMap);
                businessRuleInfoEntity.setInactiveName(inactiveRule.getName());
                businessRuleInfoEntity.setInactiveType(inactiveRule.getType());
                businessRuleInfoEntity.setInactiveAppId(ruleTargetPojo.getAppId());
                businessRuleInfoEntity.setInactiveMenuId(ruleTargetPojo.getMenuId());
                businessRuleInfoEntity.setInactiveFormId(ruleTargetPojo.getFormId());
                businessRuleInfoEntity.setInactiveSaasMark(ruleTargetPojo.getLinkSaasMark());
                businessRuleInfoEntity.setInactiveBusinessType(ruleTargetPojo.getLinkBusinessType());
                businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(rulePoJo.getFilterConditions()));
                businessRuleInfoEntity.setInactivePreConditions(JSON.toJSONString(rulePoJo.getPreConditions()));
                businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(rulePoJo.getOperations()));
                businessRuleInfoEntity.setInactiveOperationType(rulePoJo.getOperationType());
                businessRuleInfoEntity.setInactiveMemo(inactiveRule.getMemo());
                businessRuleInfoEntity.setInactiveEnable(inactiveRule.getEnable());
            } else {
                businessRuleInfoEntity.setInactiveFilterConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactivePreConditions(JSON.toJSONString(new ArrayList<>()));
                businessRuleInfoEntity.setInactiveOperations(JSON.toJSONString(new ArrayList<>()));
            }
            businessRuleInfoEntity.setDel(DelEnum.NORMAL.getDel());
            businessRuleInfoEntity.setPriority(businessRule.getPriority());
            update = businessRuleInfoModel.update(businessRuleInfoEntity);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("BusinessRuleInfoServiceImpl.add 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return update;
    }

    /**
     * 校验条件是否为同一个子表单
     * @param conditionMap
     * @param operatorMap
     * @param operationType
     * @throws XbbException
     */
    private void validateSubForm(Map<String, Set<String>> conditionMap, Map<String, Set<String>> operatorMap, Integer operationType) throws XbbException {
        if (conditionMap.isEmpty() && operatorMap.isEmpty()) {
            return;
        }
        if (Objects.equals(RuleOperationEnum.ONLY_DELETE.getType(), operationType) || Objects.equals(RuleOperationEnum.ONLY_INSERT.getType(), operationType)) {
            return;
        }
        Set<String> conditionTargetSet = conditionMap.get("target") == null ? new HashSet<>() : conditionMap.get("target");
        Set<String> operatorTargetSet = operatorMap.get("target") == null ? new HashSet<>() : operatorMap.get("target");
        Set<String> conditionThisSet = conditionMap.get("this") == null ? new HashSet<>() : conditionMap.get("this");
        Set<String> operatorThisSet = operatorMap.get("this") == null ? new HashSet<>() : operatorMap.get("this");
        if (conditionTargetSet.size() > 0 && operatorTargetSet.size() > 0 && conditionTargetSet.size() != operatorTargetSet.size()) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261016);
        }
        if (conditionTargetSet.size() == 1 && conditionTargetSet.size() == operatorTargetSet.size() && !conditionTargetSet.containsAll(operatorTargetSet)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261016);
        }
        if (conditionThisSet.size() > 0 && operatorThisSet.size() > 0 && conditionThisSet.size() != operatorThisSet.size()) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261016);
        }
        if (conditionThisSet.size() == 1 && conditionThisSet.size() == operatorThisSet.size() && !conditionThisSet.containsAll(operatorThisSet)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261016);
        }
        // 目标字段的条件没有子表单字段，操作中目标表单有子表单字段得提示
        if (conditionTargetSet.size() == 0 && operatorTargetSet.size() > 0) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261016);
        }
        // 条件有固定值的情况下，操作可以有子表单字段
        if (conditionThisSet.size() == 0 && conditionThisSet.size() != operatorThisSet.size()) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261016);
        }
    }

    /**
     * 校验前置过滤条件
     * @param preConditionList
     * @return void
     * @author 吴峰
     * @date 2021/1/8 14:54
     * @throws XbbException
     */
    private static void validatePreCondition(List<FilterConditionsPoJo> preConditionList, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        if (CollectionsUtil.isEmpty(preConditionList)) {
            return;
        }
        for (FilterConditionsPoJo item : preConditionList) {
            String symbol = item.getSymbol();
            if (Objects.equals(ConditionEnum.EMPTY.getSymbol(), symbol) || Objects.equals(ConditionEnum.NOEMPTY.getSymbol(), symbol)) {
                continue;
            }
            FieldAttrEntity fieldAttrEntity = explainMap.get(item.getAttr());
            Object value = item.getValue();
            if (Objects.isNull(value)) {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261025, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261025.getMsg(), fieldAttrEntity.getAttrName()));
            } else if (Objects.equals(ConditionEnum.RANGE.getSymbol(), item.getSymbol())) {
                JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(value));
                if (Objects.isNull(jsonObject.get(0) ) || Objects.isNull(jsonObject.get(1))) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261025, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261025.getMsg(), fieldAttrEntity.getAttrName()));
                }
            }else if (Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldAttrEntity.getFieldType())) {
                JSONObject targetJson = JSON.parseObject(JSON.toJSONString(value));
                String targetProvince = targetJson.getString(BusinessConstant.PROVINCE);
                if (StringUtil.isEmpty(targetProvince)) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261025, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261025.getMsg(), fieldAttrEntity.getAttrName()));
                }
            } else if (Objects.equals(FieldTypeEnum.RADIO_GROUP.getType(), fieldAttrEntity.getFieldType())) {
                if (value instanceof ItemPoJo || value instanceof Map) {
                    ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(value), ItemPoJo.class);
                    boolean otherIsEmpty = Objects.equals(1, itemPoJo.getIsOther()) && (Objects.isNull(itemPoJo.getValue()) || StringUtil.isEmpty(itemPoJo.getValue().toString()));
                    if (otherIsEmpty) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261025, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261025.getMsg(), fieldAttrEntity.getAttrName()));
                    }
                } else if (value instanceof List) {
                    List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(value), ItemPoJo.class);
                    for (ItemPoJo itemPoJo : itemList) {
                        boolean otherIsEmpty = Objects.equals(1, itemPoJo.getIsOther()) && (Objects.isNull(itemPoJo.getValue()) || StringUtil.isEmpty(itemPoJo.getValue().toString()));
                        if (otherIsEmpty) {
                            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261025, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261025.getMsg(), fieldAttrEntity.getAttrName()));
                        }
                    }
                }

            }
            boolean flag = AbstractSymbolHandle.noEmpty(value);
            if (!flag) {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261025, String.format(BusinessRuleErrorCodeEnum.API_ERROR_261025.getMsg(), fieldAttrEntity.getAttrName()));
            }
        }
    }

    /**
     * 判断过滤条件
     * @author 吴峰
     * @param rule 规则
     * @param type 1：简易模式 2：复杂模式
     * @param explainMap 字段容器
     * @param targetExplainMap 目标字段容器
     * @param workOrderExplainMap 工单容器
     * @date 2020-02-11 15:15
     * @throws XbbException 业务异常
     */
    private static Map<String, Set<String>> validateFilterCondition(RulePoJo rule, Integer type, Map<String, FieldAttrEntity> explainMap,
                                                             Map<Long, Map<String, FieldAttrEntity>> targetExplainMap, Map<Long, Map<String, FieldAttrEntity>> workOrderExplainMap,
                                                             Long formId, Integer businessType) throws XbbException {
        // 简易模式以及高级模式不等于新增时判断条件
        Map<String, Set<String>> attrListMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (!Objects.equals(RuleOperationEnum.ONLY_INSERT.getType(), rule.getOperationType())) {
            if (CollectionUtils.isEmpty(rule.getFilterConditions())) {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261005);
            }
            // 当前表单的子表单
            Set<String> thisSubFormSet = new HashSet<>();
            // 目标表单的子表单
            Set<String> targetSubFormSet = new HashSet<>();
            boolean isSubForm = false;
            boolean isMainField = false;
            boolean isMainSub = false;
            boolean isSubMain = false;
            Set<String> targetFieldSet = new HashSet<>();
            Set<String> thisFieldSet = new HashSet<>();
            RuleTargetPojo ruleTargetPojo = rule.getTarget();
            Map<String, FieldAttrEntity> thisSubExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<String, FieldAttrEntity> targetSubExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<String, FieldAttrEntity> targetExplain = getExplainMapByFormId(ruleTargetPojo, workOrderExplainMap, targetExplainMap);
            for (FilterConditionsPoJo item : rule.getFilterConditions()) {
                if (StringUtil.isEmpty(item.getAttr())) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261001);
                }
                String attr = item.getAttr();
                String targetSubForm = "";
                FieldAttrEntity targetField = null;
                // 判断子表单字段，获取对应的字段
                if (attr.contains(".")) {
                    String[] attrArray = attr.split("\\.");
                    attr = attrArray[0];
                    targetSubForm = attrArray[1];
                    targetSubFormSet.add(attr);
                    targetFieldSet.add(attr);
                }

                targetField = targetExplain.get(attr);
                if (Objects.equals(item.getValueType(), RuleValueTypeEnum.DYNAMIC_VALUE.getType())) {

                    String valueAttr = item.getValueAttr();
                    String thisSubForm = "";
                    // 判断子表单字段，获取对应的字段
                    if (valueAttr.contains(".")) {
                        String[] attrArray = valueAttr.split("\\.");
                        valueAttr = attrArray[0];
                        thisSubForm = attrArray[1];
                        thisSubFormSet.add(valueAttr);
                        thisFieldSet.add(valueAttr);
                    }
                    if (StringUtil.isEmpty(valueAttr)) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261001);
                    }


                    FieldAttrEntity fieldAttrEntity = explainMap.get(valueAttr);
                    if (Objects.isNull(fieldAttrEntity)) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                    }

                    if (Objects.isNull(targetField)) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                    }
                    if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType()) && ExplainUtil.isSubForm(targetField.getFieldType())) {
                        isSubForm = true;
                    }
                    if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType()) && !ExplainUtil.isSubForm(targetField.getFieldType())) {
                        isMainSub = true;
                    }
                    if (!ExplainUtil.isSubForm(fieldAttrEntity.getFieldType()) && ExplainUtil.isSubForm(targetField.getFieldType())) {
                        isSubMain = true;
                    }
                    if (!ExplainUtil.isSubForm(targetField.getFieldType()) && !ExplainUtil.isSubForm(fieldAttrEntity.getFieldType()) && !isMainField) {
                        isMainField = true;
                    }
                    if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType()) && thisSubExplainMap.isEmpty()) {
                        List<? extends FieldAttrEntity> jsonArray = fieldAttrEntity.getSubForm().getItems();
                        thisSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                    }
                    if (StringUtil.isNotEmpty(thisSubForm)) {
                        fieldAttrEntity = thisSubExplainMap.get(thisSubForm);
                    }
                    if (ExplainUtil.isSubForm(targetField.getFieldType()) && targetSubExplainMap.isEmpty()) {
                        List<? extends FieldAttrEntity> jsonArray = targetField.getSubForm().getItems();
                        targetSubExplainMap= ExplainUtil.getExplainMapByList(jsonArray);
                    }
                    if (StringUtil.isNotEmpty(targetSubForm)) {
                        targetField = targetSubExplainMap.get(targetSubForm);
                    }
                    // 校验关联数据、系统关联字段和dataId之间的关联关系
                    ConditionUtil.validateTheForm(targetField, fieldAttrEntity, formId, ruleTargetPojo.getFormId(), ruleTargetPojo.getLinkBusinessType(),   businessType);
                } else {
                    // 固定值
                    if (Objects.isNull(item.getValue()) || StringUtil.isEmpty(item.getValue().toString())) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261005);
                    }
                    if (!ExplainUtil.isSubForm(targetField.getFieldType())) {
                        isMainField = true;
                    }
                }
            }
            if (thisSubFormSet.size() > 1) {
                // 过滤条件仅支持选择同一个子表单
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261008);
            }
            if (targetSubFormSet.size() > 1) {
                // 过滤条件仅支持选择同一个子表单
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261009);
            }
            if (isSubForm && !isMainField) {
                // 若过滤条件含子赋值子，则过滤条件须再添加一组主赋值主
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261010);
            }
            // 子赋值主isMainSub
            // 主赋值子isSubMain
            // 子赋值子 isSubForm
            boolean flag = (isMainSub && isSubMain) || (isMainSub && isSubForm) || (isSubMain && isSubForm);
            if (flag) {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261011);
            }
            attrListMap.put("target", targetFieldSet);
            attrListMap.put("this", thisFieldSet);
        }
        return attrListMap;
    }

    private Map<String, Set<String>> validateOperate(String corpid, RulePoJo rule, Integer type, Map<String, FieldAttrEntity> explainMap, Map<Long, Map<String, FieldAttrEntity>> targetExplainMap, Map<Long, Map<String, FieldAttrEntity>> workOrderExplainMap, Long formId, Integer businessType) throws XbbException {
        Map<String, Set<String>> attrFieldSet = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.equals(RuleOperationEnum.ONLY_DELETE.getType(), rule.getOperationType())) {
            return attrFieldSet;
        }
        List<OperationsPojo> operationList = rule.getOperations();
        // 操作不能为空
        if (CollectionUtils.isEmpty(operationList)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261004);
        }
        RuleTargetPojo ruleTargetPojo = rule.getTarget();
        Map<String, FieldAttrEntity> targetExplain = getExplainMapByFormId(ruleTargetPojo, workOrderExplainMap, targetExplainMap);
        if (Objects.equals(type, RuleVersionEnum.SIMPLE.getType())) {
            // 简易模式

            Set<String> targetFieldSet = new HashSet<>();
            Set<String> thisFieldSet = new HashSet<>();
            for (OperationsPojo item : operationList) {
                String targetAttr = item.getAttr();
                if (StringUtil.isEmpty(targetAttr)) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261002);
                }
                boolean isFixed = (Objects.isNull(item.getValue()) || StringUtil.isEmpty(item.getValue().toString()))
                        && Objects.equals(RuleValueTypeEnum.FIXED_VALUE.getType(), item.getValueType());
                List<String> targetFieldList = ExplainUtil.getAttrByPoint(targetAttr);
                String targetSubForm = "";
                if (targetFieldList.size() == 2) {
                    targetAttr = targetFieldList.get(0);
                    targetSubForm = targetFieldList.get(1);
                    targetFieldSet.add(targetAttr);
                }
                if (Objects.equals(item.getValueType(), RuleValueTypeEnum.DYNAMIC_VALUE.getType())) {
                    // 动态值
                    if (StringUtil.isEmpty(item.getValueAttr())) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261001);
                    }
                    String thisAttr = item.getValueAttr();
                    List<String> thisFieldList = ExplainUtil.getAttrByPoint(thisAttr);
                    String thisSubForm = "";
                    if (thisFieldList.size() == 2) {
                        thisAttr = thisFieldList.get(0);
                        thisSubForm = thisFieldList.get(1);
                        thisFieldSet.add(thisAttr);
                    }
                    boolean hasInsert = Objects.equals(RuleOperationEnum.ONLY_INSERT.getType(), rule.getOperationType())
                            || Objects.equals(RuleOperationEnum.UPDATE_AND_INSERT.getType(), rule.getOperationType());
                    if (targetFieldList.size() != thisFieldList.size() && targetFieldList.size() == 2 && hasInsert) {
                        // 目标表单为子表单时，不可以设置主字段，请重新选择
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261015);
                    }

                    FieldAttrEntity targetField = targetExplain.get(targetAttr);
                    if (Objects.isNull(targetField)) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                    }
                    if (ExplainUtil.isSubForm(targetField.getFieldType())) {
                        Map<String, FieldAttrEntity> targetSubFormMap = ExplainUtil.getExplainMapByList(targetField.getSubForm().getItems());
                        targetField = targetSubFormMap.get(targetSubForm);
                    }
                    FieldAttrEntity fieldAttrEntity = new FieldAttrEntity();
                    if (Objects.equals(FieldTypeEnum.DATAID.getType(), item.getValueFieldType())) {
                        fieldAttrEntity.setAttr(FieldTypeEnum.DATAID.getAlias());
                        fieldAttrEntity.setAttrName(I18nMessageUtil.getMessage(I18nStringConstant.SELECTED_DATA));
                        fieldAttrEntity.setFieldType(FieldTypeEnum.DATAID.getType());
                    } else if (Objects.equals(FieldTypeEnum.LINK_DATA.getType(), item.getValueFieldType())) {
                        fieldAttrEntity = explainMap.get(thisAttr);
                        if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType())) {
                            Map<String, FieldAttrEntity> thisSubExplainMap = ExplainUtil.getExplainMapByList(fieldAttrEntity.getSubForm().getItems());
                            fieldAttrEntity = thisSubExplainMap.get(thisSubForm);
                        }
                        if (Objects.isNull(fieldAttrEntity)) {
                            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                        }
                    }
                    ConditionListHelp.validateTheForm(targetField, fieldAttrEntity, formId, ruleTargetPojo.getFormId(), ruleTargetPojo.getLinkBusinessType(), businessType);
                } else  {
                    if (isFixed) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261003);
                    }
                    FieldAttrEntity targetField = targetExplain.get(targetAttr);
                    if (Objects.isNull(targetField)) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                    }
                    if (ExplainUtil.isSubForm(targetField.getFieldType())) {
                        Map<String, FieldAttrEntity> targetSubFormMap = ExplainUtil.getExplainMap(JSON.toJSONString(targetField.getSubForm().getItems()), null);
                        targetField = targetSubFormMap.get(targetSubForm);
                    }
                    if (Objects.isNull(targetField)) {
                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                    }
                }
                attrFieldSet.put("target", targetFieldSet);
                attrFieldSet.put("this", thisFieldSet);
            }
        } else {
            Integer feeType = businessRuleUserPermissionHelp.getFeeType(corpid);
            if (Objects.equals(1, feeType) && CollectionsUtil.isNotEmpty(operationList)) {
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261021);
            }
            Set<String> targetFieldSet = new HashSet<>();
            Set<String> thisFieldSet = new HashSet<>();
            Map<String, FieldAttrEntity> targetSubExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<String, FieldAttrEntity> thisSubExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (OperationsPojo item : operationList) {
                String targetAttr = item.getAttr();
                if (StringUtil.isEmpty(targetAttr)) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261002);
                }
                List<String> targetFieldList = ExplainUtil.getAttrByPoint(targetAttr);
                String targetSubForm = "";
                if (targetFieldList.size() == 2) {
                    targetAttr = targetFieldList.get(0);
                    targetSubForm = targetFieldList.get(1);
                    targetFieldSet.add(targetAttr);
                }
                FieldAttrEntity targetField = targetExplain.get(targetAttr);
                if (Objects.isNull(targetField)) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                }
                if (ExplainUtil.isSubForm(targetField.getFieldType())) {
                    Map<String, FieldAttrEntity> targetSubFormMap = ExplainUtil.getExplainMap(JSON.toJSONString(targetField.getSubForm().getItems()), null);
                    targetField = targetSubFormMap.get(targetSubForm);
                }
                if (Objects.isNull(targetField)) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                }
                if (Objects.isNull(item.getFormulaInfo()) || StringUtil.isEmpty(item.getFormulaInfo().getFormula())) {
                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261004);
                }
                List<String> targetList = ExplainUtil.getAttrByPoint(targetAttr);
                if (targetList.size() == 2) {
                    targetFieldSet.add(targetList.get(0));
                }
                // 目标表单的容器
                String formula = item.getFormulaInfo().getFormula();
                String[] formulaArray = RegexUtil.getFormulaAttr(formula);
                for (int i = 0; i < formulaArray.length; i++) {
                    String formulaAttr = formulaArray[i];
                    String attr = "";
                    if (formulaAttr.contains("self.")) {
                        // 本表单的字段操作
                        attr = formulaAttr.replaceAll("self.", "").replaceAll("[\\{\\}]", "");

                        if (attr.contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                            // 子表单条件对比
                            List<String> thisList = ExplainUtil.getAttrByPoint(attr);
                            if (thisList.size() == 2) {
                                attr = thisList.get(0);
                                String thisSubForm = thisList.get(1);
                                thisFieldSet.add(attr);
                                FieldAttrEntity fieldAttrEntity = explainMap.get(attr);
                                if (Objects.isNull(fieldAttrEntity)) {
                                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                                }
                                if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType())) {
                                    if (thisSubExplainMap.isEmpty()) {
                                        thisSubExplainMap = ExplainUtil.getExplainMap(JSON.toJSONString(fieldAttrEntity.getSubForm().getItems()), null);
                                    }
                                    fieldAttrEntity = thisSubExplainMap.get(thisSubForm);
                                    if (Objects.isNull(fieldAttrEntity)) {
                                        throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                                    }
                                }
                            }
                        } else {
                            FieldAttrEntity fieldAttrEntity = explainMap.get(attr);
                            if (Objects.isNull(fieldAttrEntity)) {
                                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                            }
                        }
                    } else if (RegexUtil.isStartWithNumber(formulaAttr) || formulaAttr.contains("workOrder_")) {
                        // 目标表单
                        Integer startNum = formulaAttr.indexOf(".")+1;
                        Integer length = formulaAttr.length();
                        attr = formulaAttr.substring(startNum, length).replaceAll("[\\{\\}]", "");
                        if (attr.contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                            targetList = ExplainUtil.getAttrByPoint(attr);
                            if (targetList.size() == 2) {
                                attr = targetList.get(0);
                                FieldAttrEntity fieldAttrEntity = targetExplain.get(attr);
                                if (Objects.isNull(fieldAttrEntity)) {
                                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                                }
                                String targetSubAttr = targetList.get(1);
                                targetFieldSet.add(attr);
                                if (targetSubExplainMap.isEmpty()) {
                                    targetSubExplainMap = ExplainUtil.getExplainMap(JSON.toJSONString(fieldAttrEntity.getSubForm().getItems()), null);
                                }
                                if (Objects.isNull(targetSubExplainMap.get(targetSubAttr))) {
                                    throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                                }
                            }
                        } else {
                            FieldAttrEntity fieldAttrEntity = targetExplain.get(attr);
                            if (Objects.isNull(fieldAttrEntity)) {
                                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261006);
                            }
                        }
                    }
                }
            }
            boolean hasInsert = Objects.equals(RuleOperationEnum.ONLY_INSERT.getType(), rule.getOperationType())
                    || Objects.equals(RuleOperationEnum.UPDATE_AND_INSERT.getType(), rule.getOperationType());
            if (targetFieldSet.size() != thisFieldSet.size() && targetFieldSet.size() == 1 && thisFieldSet.size() != 0 && hasInsert) {
                // 仅插入和插入更新时 目标表单为子表单时，不可以设置主字段，请重新选择
                throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261015);
            }
            attrFieldSet.put("target", targetFieldSet);
            attrFieldSet.put("this", thisFieldSet);
        }
        return attrFieldSet;
    }


    private static Map<String, FieldAttrEntity> getExplainMapByFormId(RuleTargetPojo ruleTargetPojo, Map<Long, Map<String, FieldAttrEntity>> workOrderExplainMap, Map<Long, Map<String, FieldAttrEntity>> targetExplainMap) {
        Map<String, FieldAttrEntity> targetExplain = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), ruleTargetPojo.getLinkBusinessType())) {
            targetExplain = workOrderExplainMap.get(ruleTargetPojo.getFormId());
        } else {
            targetExplain = targetExplainMap.get(ruleTargetPojo.getFormId());
        }
        ProFormHelp.setDataIdAttr(targetExplain);
        return targetExplain;
    }

    private String getFormName(Integer businessType, Long formId, Map<Long, WorkOrderFormEntity> workOrderFormMap, Map<Long, PaasFormEntityExt> formMap) {
        if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode())) {
            WorkOrderFormEntity paasFormEntityExt = workOrderFormMap.get(formId);
            if (Objects.isNull(paasFormEntityExt)) {
                return "";
            }
            return paasFormEntityExt.getName();
        } else {
            PaasFormEntityExt paasFormEntityExt = formMap.get(formId);
            if (Objects.isNull(paasFormEntityExt)) {
                return "";
            }
            return paasFormEntityExt.getName();
        }
    }

    private void getFormIdIn(Long formId, Integer businessType, Set<Long> formIdIn, Set<Long> workOrderFormIdIn) {
        if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode())) {
            workOrderFormIdIn.add(formId);
        } else {
            formIdIn.add(formId);
        }
    }

    private void getFormMap(Set<Long> formIdIn, Set<Long> workOrderFormIdIn, String corpid, Map<Long, WorkOrderFormEntity> workOrderFormMap, Map<Long, PaasFormEntityExt> formMap) throws XbbException {
        if (!workOrderFormIdIn.isEmpty()) {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put(StringConstant.CORPID, corpid);
            param.put("idIn", workOrderFormIdIn);
            param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<WorkOrderFormEntity> formList = workOrderFormModel.findEntitys(param);
            if (Objects.isNull(formList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_NOT_EXIST);
            }
            formList.forEach(item->{
                workOrderFormMap.put(item.getId(), item);
            });
        }

        if (!formIdIn.isEmpty()) {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put(StringConstant.CORPID, corpid);
            param.put("idIn", formIdIn);
            param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            List<PaasFormEntityExt> formList = paasFormModel.list(param);
            if (Objects.isNull(formList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_NOT_EXIST);
            }
            formList.forEach(item->{
                formMap.put(item.getId(), item);
            });
        }
    }

    private Boolean setPackageLimit(ActiveRulePoJo activeRulePoJo, Integer feeType) {
        if (Objects.equals(RuleVersionEnum.ADVANCED.getType(), activeRulePoJo.getType())
                && Objects.equals(PackageTypeEnum.ADVANCED.getType(), feeType)) {
            activeRulePoJo.setPackageLimit(1);
            activeRulePoJo.setMessage(I18nMessageUtil.getMessage(PackageConstant.BUSINESS_RULE_FORMULA_MESSAGE));
            return true;
        }
        return false;
    }
}
